package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.GwAgentCommission;
import com.ruoyi.system.domain.GwSector;
import com.ruoyi.system.domain.vo.RightsVO;
import com.ruoyi.system.mapper.GwAgentCommissionMapper;
import com.ruoyi.system.mapper.GwSectorMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GwSystemConfigMapper;
import com.ruoyi.system.domain.GwSystemConfig;
import com.ruoyi.system.service.IGwSystemConfigService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 系统运营配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class GwSystemConfigServiceImpl implements IGwSystemConfigService 
{
    @Resource
    private GwSystemConfigMapper gwSystemConfigMapper;
    @Resource
    private GwAgentCommissionMapper agentCommissionMapper;
    @Resource
    private GwSectorMapper gwSectorMapper;

    /**
     * 查询系统运营配置
     * 
     * @param id 系统运营配置主键
     * @return 系统运营配置
     */
    @Override
    public GwSystemConfig selectGwSystemConfigById(String id)
    {
        return gwSystemConfigMapper.selectGwSystemConfigById(id);
    }

    /**
     * 查询系统运营配置列表
     * 
     * @param gwSystemConfig 系统运营配置
     * @return 系统运营配置
     */
    @Override
    public List<GwSystemConfig> selectGwSystemConfigList(GwSystemConfig gwSystemConfig)
    {
        List<GwSystemConfig> list = gwSystemConfigMapper.selectGwSystemConfigList(gwSystemConfig);
        if(!CollectionUtils.isEmpty(list)){
            List<GwSector> sectorList = gwSectorMapper.selectGwSectorList(new GwSector());
            list.get(0).setSectorList(sectorList);

            List<GwAgentCommission> commissionList = agentCommissionMapper.selectGwAgentCommissionList(new GwAgentCommission());
            if (!CollectionUtils.isEmpty(commissionList)) {
                List<RightsVO> rightsVOList = Lists.newArrayList();
                commissionList.forEach(k->{
                    RightsVO vo = new RightsVO();
                    vo.setBusinessType(k.getBusinessType());
                    vo.setRightsId(k.getPlatformRightsId());
                    rightsVOList.add(vo);
                });
                list.get(0).setRightList(rightsVOList);
            }
        }
        return list;
    }

    /**
     * 新增系统运营配置
     * 
     * @param gwSystemConfig 系统运营配置
     * @return 结果
     */
    @Override
    public int insertGwSystemConfig(GwSystemConfig gwSystemConfig)
    {
        gwSystemConfig.setId(IdUtils.fastSimpleUUID());
        gwSystemConfig.setCreateTime(DateUtils.getNowDate());
        if(!StringUtils.isBlank(gwSystemConfig.getPurchaseAgreement())){
            gwSystemConfig.setPurchaseFileName(getFileName(gwSystemConfig.getPurchaseAgreement()));
        }
        int num = gwSystemConfigMapper.insertGwSystemConfig(gwSystemConfig);
        if(!CollectionUtils.isEmpty(gwSystemConfig.getRightList())){
            gwSystemConfig.getRightList().forEach(k->{
                GwAgentCommission commission = new GwAgentCommission();
                commission.setId(IdUtils.fastSimpleUUID());
                commission.setBusinessType(k.getBusinessType());
                commission.setPlatformRightsId(k.getRightsId());
                agentCommissionMapper.insertGwAgentCommission(commission);
            });
        }

        if(!CollectionUtils.isEmpty(gwSystemConfig.getSectorList())){
            gwSystemConfig.getSectorList().forEach(k->{
                k.setId(IdUtils.fastSimpleUUID());
                gwSectorMapper.insertGwSector(k);
            });
        }

        return num;
    }

    /**
     * 修改系统运营配置
     * 
     * @param gwSystemConfig 系统运营配置
     * @return 结果
     */
    @Override
    public int updateGwSystemConfig(GwSystemConfig gwSystemConfig)
    {
        gwSystemConfig.setUpdateTime(DateUtils.getNowDate());
        int num = gwSystemConfigMapper.updateGwSystemConfig(gwSystemConfig);

        //处理代理商平台权益分佣配置
        List<GwAgentCommission> commissionList = agentCommissionMapper.selectGwAgentCommissionList(new GwAgentCommission());
        if(!CollectionUtils.isEmpty(commissionList)){
            commissionList.forEach(k->{
                agentCommissionMapper.deleteGwAgentCommissionById(k.getId());
            });
        }
        if(!CollectionUtils.isEmpty(gwSystemConfig.getRightList())){
            gwSystemConfig.getRightList().forEach(k->{
                GwAgentCommission commission = new GwAgentCommission();
                commission.setId(IdUtils.fastSimpleUUID());
                commission.setBusinessType(k.getBusinessType());
                commission.setPlatformRightsId(k.getRightsId());
                agentCommissionMapper.insertGwAgentCommission(commission);
            });
        }

        //处理行业字典（键值对采用行业名称-行业名称）
        List<GwSector> sectorList = gwSectorMapper.selectGwSectorList(new GwSector());
        sectorList.forEach(k->{
            gwSectorMapper.deleteGwSectorById(k.getId());
        });

        gwSystemConfig.getSectorList().forEach(k->{
            k.setId(IdUtils.fastSimpleUUID());
            gwSectorMapper.insertGwSector(k);
        });

        return num;
    }

    /**
     * 批量删除系统运营配置
     * 
     * @param ids 需要删除的系统运营配置主键
     * @return 结果
     */
    @Override
    public int deleteGwSystemConfigByIds(String[] ids)
    {
        return gwSystemConfigMapper.deleteGwSystemConfigByIds(ids);
    }

    /**
     * 删除系统运营配置信息
     * 
     * @param id 系统运营配置主键
     * @return 结果
     */
    @Override
    public int deleteGwSystemConfigById(String id)
    {
        return gwSystemConfigMapper.deleteGwSystemConfigById(id);
    }

    @Override
    public GwSystemConfig getSystemConfigInfo() {
        List<GwSystemConfig> configList =  gwSystemConfigMapper.selectGwSystemConfigList(new GwSystemConfig());
        if(!CollectionUtils.isEmpty(configList)){
            return configList.get(0);
        }
        return null;
    }

    private static String getFileName(String filePath){
        if(StringUtils.isBlank(filePath)){
            return null;
        }
        String fileName = "";
        if(filePath.lastIndexOf("/")!=-1){
            filePath = filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
        }
        String[] names = filePath.split("_");
        if(names!=null && names.length>0){
            fileName = names[0];
        }

        return fileName;
    }

}
